
using System;
using System.Collections.Generic;
using System.Text;
using ScrewTurn.Wiki.PluginFramework;

namespace ScrewTurn.Wiki {

	/// <summary>
	/// Manages navigation Breadcrumbs.
	/// </summary>
	[Serializable]
	public class BreadcrumbsManager {

		private static int maxPages = 10;

		private List<PageInfo> pages;

		/// <summary>
		/// Initializes a new instance of the <b>BreadcrumbsManager</b> class.
		/// </summary>
		public BreadcrumbsManager() {
			pages = new List<PageInfo>();
		}

		/// <summary>
		/// Adds a Page to the Breadcrumbs trail.
		/// </summary>
		/// <param name="page">The Page to add.</param>
		public void AddPage(PageInfo page) {
			lock(pages) {
				if(pages.Count == 0 || (pages.Count > 0 && !pages.Contains(page))) pages.Add(page);
				if(pages.Count > maxPages) pages.RemoveRange(0, pages.Count - maxPages);
			}
		}

		/// <summary>
		/// Removes a Page from the Breadcrumbs trail.
		/// </summary>
		/// <param name="page">The Page to remove.</param>
		public void RemovePage(PageInfo page) {
			lock(pages) {
				pages.Remove(page);
			}
		}

		/// <summary>
		/// Clears the Breadcrumbs trail.
		/// </summary>
		public void Clear() {
			lock(pages) {
				pages.Clear();
			}
		}

		/// <summary>
		/// Gets all the Pages in the trail that still exist.
		/// </summary>
		public PageInfo[] AllPages {
			get {
				lock(pages) {

					List<PageInfo> newPages = new List<PageInfo>(pages.Count);
					foreach(PageInfo p in pages) {
						if(Pages.Instance.FindPage(p.Name) != null) newPages.Add(p);
					}

					pages = newPages;

					PageInfo[] array;
					array = pages.ToArray();
					return array;
				}
			}
		}

	}

}
